<?php
/**
 * Skyline :: Patterns & component framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Skyline\Structure\Multiset;

use ArrayIterator;
use Skyline\Comparator\ComparatorInterface;
use Skyline\Predicate\PredicateInterface;
use Skyline\Structure\AbstractCollection;
use Skyline\Structure\BoundContainerInterface;

/**
 * Abstract implementation of a multiset that is backed by a map
 * 
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
abstract class AbstractMapMultiset extends AbstractCollection implements MultisetInterface
{
	/**
	 * @var MapInterface
	 */
	protected $map;
	
	/**
	 * Class constructor
	 * 
	 * @param Traversable $elements [optional]
	 * @return void
	 */
	public function __construct($elements = null)
	{
		$this->map = $this->createMap();
		
		if(!is_null($elements)) {
			$this->addAll($elements);
		}
	}
	
	/**
	 * Add an element to the collection
	 * 
	 * @param mixed $element
	 * @return void
	 */
	public function add($element)
	{
		$this->addCount($element, 1);
	}
	
	/**
	 * Adds an element <i>n</i> number of times
	 * 
	 * @param mixed $element
	 * @param int $count
	 * @return void
	 */
	public function addCount($element, $count)
	{
		if(!$this->map->containsKey($element)) {
			$this->setCount($element, $count);
			return;
		}
		$this->map->set($element, ($this->map->get($element) + $count));
	}
	
	/**
	 * Clears this container of all its elements
	 * @return void
	 */
	public function clear()
	{
		$this->map->clear();
	}
	
	/**
	 * Returns true if an element exists in the collection
	 * 
	 * @param mixed $element
	 * @return bool
	 */
	public function contains($element)
	{
		return $this->map->containsKey($element);
	}
	
	/**
	 * Returns the count of a given element
	 * 
	 * @param mixed $element
	 * @return int
	 */
	public function getCount($element)
	{
		return (int)$this->map->get($element);
	}
	
	/**
	 * Returns a unique set of the elements in this multiset
	 * @return SetInterface
	 */
	public function getSet()
	{
		return $this->map->getKeys();
	}
	
	/**
	 * Removes an element from the collection
	 * 
	 * @param mixed $element
	 * @return void
	 */
	public function remove($element)
	{
		$this->removeCount($element, 1);
	}
	
	/**
	 * Removes an element <i>n</i> number of times
	 * 
	 * @param mixed $element
	 * @param int $count
	 * @return void
	 */
	public function removeCount($element, $count)
	{
		if($this->map->containsKey($element)) {
			$newCount = ($this->map->get($element) - $count);
			if($newCount <= 0) {
				$this->map->remove($element);
				return;
			}
			$this->map->set($element, $newCount);
		}
	}
	
	/**
	 * Sets the count of the element to <i>n</i>
	 * 
	 * @param mixed $element
	 * @param int $count
	 * @return void
	 */
	public function setCount($element, $count)
	{
		$this->map->set($element, $count);
	}
	
	/**
	 * Sets the count of the element to <i>newCount</i> <b>if</b> its previous
	 * count is equal to <i>oldCount</i>
	 * 
	 * @param mixed $element
	 * @param int $oldCount
	 * @param int $newCount
	 * @return bool Returns true if operation was successful
	 */
	public function setCountIf($element, $oldCount, $newCount)
	{
		if($this->map->get($element) === $oldCount) {
			$this->map->set($element, $newCount);
			return true;
		}
		return false;
	}
	
	/**
	 * Returns this collection as a simple PHP array
	 * @return mixed[]
	 */
	public function toArray()
	{
		$entries = $this->map->toArray();
		$elements = array();
		foreach($entries as $element => $count) {
			$elements = array_merge($elements, array_fill(0, $count, $element));
		}
		return $elements;
	}
	
	/**
	 * Returns the number of elements in the multiset
	 * @return int
	 */
	public function count()
	{
		return $this->map->count();
	}
	
	/**
	 * Returns an iterator for the elements in the multiset
	 * @return Iterator
	 */
	public function getIterator()
	{
		return new ArrayIterator($this->toArray());
	}
	
	/**
	 * Sets the comparator used to sort this set
	 * 
	 * @param ComparatorInterface $comparator
	 * @return void
	 */
	public function setComparator(ComparatorInterface $comparator)
	{
		$this->map->setComparator($comparator);
	}
	
	/**
	 * Sets the maximum number of elements the container can hold
	 * 
	 * @param int $capacity
	 * @return void
	 */
	public function setCapacity($capacity)
	{
		if($this->map instanceof BoundContainerInterface) {
			$this->map->setCapacity($capacity);
		}
	}
	
	/**
	 * Sets the predicate used to validate this collection
	 * 
	 * @param PredicateInterface $predicate
	 * @return void
	 */
	public function setPredicate(PredicateInterface $predicate)
	{
		$this->map->setKeyPredicate($predicate);
	}
	
	/**
	 * Creates a map that will back this multiset
	 * @return MapInterface<mixed:int>
	 */
	abstract protected function createMap();
}